package com.demohomework.homeworkdemo.controller;

import com.demohomework.homeworkdemo.pojo.Class;
import com.demohomework.homeworkdemo.pojo.Result;
import com.demohomework.homeworkdemo.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private ClassService classService;

    //新增班级
    @PostMapping
    public Result add(@RequestBody @Validated({Class.Add.class}) Class cls) {
        classService.add(cls);
        return Result.success();
    }

    //班级信息
    @GetMapping("/list")
    public Result<List<Class>> list() {
        List<Class> courses = classService.list();
        return Result.success(courses);
    }
    //根据id查询班级
    @GetMapping("/select")
    public Result<Class> show(Integer id ) {
        Class c = classService.findId(id);
        return Result.success(c);
    }
    //根据学生id查询班级
    @GetMapping("/{stuId}")
    public Result<List<Class>> showByStudentId(@Validated @PathVariable("stuId") Integer stuId ) {
        List<Class> courses = classService.findByStudentId(stuId);
        return Result.success(courses);
    }
    //根据课程id查询
    @GetMapping("/course/{couId}")
    public Result<List<Class>> showByCourseId(@Validated @PathVariable("couId") Integer courseId ) {
        List<Class> courses = classService.findByCourseId(courseId);
        return Result.success(courses);
    }
    //根据课程id和学生id删除选课
    @DeleteMapping("/{stuId}/{couId}")
    public Result deleteByStudentIdAndCourseId(@Validated @PathVariable("stuId") Integer stuId, @Validated @PathVariable("couId") Integer couId) {
        classService.deleteByStudentIdAndCourseId(stuId, couId);
        return Result.success();
    }
    //更新班级
    @PutMapping
    public Result update(@RequestBody @Validated(Class.Update.class) Class cls){
        classService.update(cls);
        return Result.success();
    }
    //删除班级
    @DeleteMapping
    public Result delete(@RequestBody Class cls){
        classService.delete(cls);
        return Result.success();
    }
}
